<template>
  <Table
    ref="tableRef"
    :columns="data.columns"
    :handles="data.handles"
    :toolbar="data.toolbar"
    :query="data.query"
    @get-list="getData"
  ></Table>
</template>
  
<script lang="ts" setup name="subsidyManagementList">
import Table from '@/components/table/index.vue';
import {
  PAGINGSUBSIDYTASK,
  terminate,
  pagingSubsidyTask,
} from '@/service/apis/user';
import { queryDicByParent } from '@/service/apis/api';

const router = useRouter();

const tableRef = ref();

const emit = defineEmits(['get-update']);
onActivated(() => {
  emit('get-update', (flag: boolean) => {
    if (flag) {
      tableRef.value?.getData();
    }
  });
});

const data = reactive({
  columns: [
    {
      prop: 'name',
      label: '任务名称',
    },
    {
      prop: 'taskType',
      label: '任务模式',
      dic: [] as any,
      type: Number,
      // dic: TASK_TYPE,
    },
    {
      prop: 'executeType',
      label: '执行模式',
      dic: [] as any,
      type: Number,
      // dic: EXECUTE_TYPE,
    },
    {
      prop: 'status',
      label: '任务状态',
      dic: [] as any,
      type: Number,
      // dic: TASK_SATAUS,
    },
    {
      prop: 'reason',
      label: '终止原因',
      dic: [] as any,
      type: Number,
      // dic: REASON_TYPE,
    },
    {
      prop: 'userId',
      label: '修改人',
    },
    {
      prop: 'updateTime',
      label: '修改时间',
    },
    //   {
    //     prop: 'isFrozen',
    //     label: '状态',
    //     template: (row: any, index: number) => {
    //       return row.isFrozen
    //         ? `<div style="color:#FA3939;">冻结</div>`
    //         : `<div style="color:#01C68B;">正常</div>`;
    //     },
    //   },
  ],
  handles: [
    {
      label: '详情',
      permission: 'subsidyManagementList.see',
      handle: (row: any) => {
        console.log('详情---', row);
        router.push({
          name: 'subsidyManagementDetail',
          query: { id: row.id },
        });
      },
    },
    {
      label: '修改',
      permission: 'subsidyManagementList.edit',
      disabledFun: (row: any) => {
        return row.status !== 0;
      },
      handle: (row: any) => {
        console.log('编辑---', row);
        router.push({
          name: 'subsidyManagementEdit',
          query: { id: row.id },
        });
      },
    },
    {
      label: '停止',
      permission: 'subsidyManagementList.stop',
      color: '#FA3939',
      disabledFun: (row: any) => {
        return row.status === 2 || row.status === 3;
      },
      handle: (row: any) => {
        ElMessageBox.confirm(
          '此操作将停止当前任务，任务停止后不再下发/扣回补贴，是否继续？',
          '提示',
          {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          }
        ).then(() => {
          terminate(Number(row.id)).then((res: any) => {
            tableRef.value.getData();
          });
        });
      },
    },
  ],
  toolbar: [
    {
      label: '新增',
      permission: 'subsidyManagementList.add',
      icon: 'icon-add',
      type: 'primary',
      handle: (row: any) => {
        router.push({
          name: 'subsidyManagementAdd',
        });
      },
    },
  ],
  query: [
    {
      label: '任务名称',
      prop: 'name',
      comName: 'input',
      props: {
        placeholder: '请输入任务名称',
      },
    },
    {
      label: '任务模式',
      prop: 'taskType',
      comName: 'select',
      props: {
        placeholder: '请选择任务模式',
        options: [] as any,
      },
    },
    {
      label: '任务状态',
      prop: 'status',
      comName: 'select',
      props: {
        placeholder: '请选择任务状态',
        options: [] as any,
      },
    },
    {
      label: '终止原因',
      prop: 'reason',
      comName: 'select',
      props: {
        placeholder: '请选择终止原因',
        options: [] as any,
      },
    },
    {
      label: '修改时间',
      prop: 'date',
      comName: 'date',
      props: {
        type: 'daterange',
        change: (value: any) => {
          console.log(value[0]);
        },
      },
    },
  ],
});

queryDicByParent({
  listDtos: [
    'subsidyTaskType',
    'executeType',
    'subsidyTaskStatus',
    'terminateReason',
  ],
}).then((res: any) => {
  if (res) {
    data.query[1].props.options = [...res.subsidyTaskType];
    data.query[2].props.options = [...res.subsidyTaskStatus];
    data.query[3].props.options = [...res.terminateReason];
    data.columns[1].dic = [...res.subsidyTaskType];
    data.columns[2].dic = [...res.executeType];
    data.columns[3].dic = [...res.subsidyTaskStatus];
    data.columns[4].dic = [...res.terminateReason];
  }
});

const getData = (page: any, search: any) => {
  const form = search.ruleForm;
  if (
    search.ruleForm &&
    search.ruleForm.date &&
    search.ruleForm.date.length > 1
  ) {
    form.startTime = `${search.ruleForm.date[0]} 00:00:00`;
    form.endTime = `${search.ruleForm.date[1]} 23:59:59`;
  } else {
    form.startTime = '';
    form.endTime = '';
  }
  pagingSubsidyTask(form, page).then((res: any) => {
    console.log('res---', res);
    if (page.pageNo !== 1 && (!res.dataList || res.dataList?.length === 0)) {
      page.pageNo--;
      getData(page, search);
      return;
    }
    nextTick(() => {
      tableRef.value.setTable({
        total: res.totalCount,
        table: res.dataList,
      });
    });
  });
};
</script>